package org.arch_learn.servlet_container.components.connector;

import org.arch_learn.servlet_container.pojo.Response;
import org.arch_learn.servlet_container.utils.StaticResourceUtil;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

public class Endpoint {
    private Socket socket;

    public Endpoint(Socket socket) {
        this.socket = socket;
    }

    public InputStream read() throws IOException {
        return socket.getInputStream();
    }

    public void close() throws IOException {
        socket.close();
    }

    public void write(Response response) throws IOException {
        socket.getOutputStream().write(response.getResponseText().getBytes());
    }

    public void writeStaticResources(Response response) throws IOException {
        if (response.getFileStream() != null) {
            StaticResourceUtil.outPutResource(response.getFileStream(), socket.getOutputStream());
        } else {
            write(response);
        }
    }


}
